<?php

namespace app\command\controller;

use app\common\func\Constant;
use app\common\func\Lib;
use app\common\func\Libs;
use app\common\func\Tools;
use app\common\model\FlowInfo;
use app\common\model\StandardRecord;
use app\data\controller\Trans;
use think\Controller;

class Jl extends Controller{

    private $baseUrl = "./trade_log/jl/";

    public function order(){
        $data = request()->param();

        file_put_contents($this->baseUrl."order_log".date("Ym").".txt",json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);

        if (is_string($data)) {
            $data = json_decode($data, true);
        }
        if (is_array($data)) {
            $data = $data;
        }

        $sn = $data['pos_sn'];
        $equip = Lib::getTerminal($sn);
        if($equip){
            try {
                $amount = bcdiv($data['amount'],'100',2);
                $transactionFee = bcdiv($data['fee_amount'],'100',2);
                $rate = round(bcdiv($transactionFee,$amount,6) * 100,2);
                $tradingTime = strtotime($data['trans_time']);

                $jl = new \app\common\model\transaction\Jl();
                $jl->agent_id = $data['agent_id'];
                $jl->busi_type = $data['busi_type'];
                $jl->busi_sub_type = $data['busi_sub_type'];
                $jl->trade_num = $data['trans_id'];
                $jl->order_num = $data['order_id'];
                $jl->sn = $data['pos_sn'];
                $jl->term_no = $data['term_no'];
                $jl->trading_time = $tradingTime;
                $jl->transaction_amount = $amount;
                $jl->transaction_rate = $rate;
                $jl->transaction_fee = $transactionFee;
                $jl->transaction_type = $data['pay_type'];
                $jl->card_no = $data['card_no'];
                $jl->ret_code = $data['ret_code'];
                $jl->ret_msg = $data['ret_msg'];
                $jl->gather_type = "NORMAL";
                $jl->save();

                Trans::instance()->customer_month($sn,$amount,strtotime($data['trans_time']));

                $arriveAmount = $amount - $transactionFee;
                $howMany = Lib::getFlowHowMany($sn);
                if($amount >= 1000 && $howMany == 0){
                    $arriveAmount -= Constant::$flowJl;
                }
                Tools::sendTran($equip->user_id,$rate * 100,"嘉联",$amount,$arriveAmount,date("Y-m-d H:i",$tradingTime),$sn);

            }catch (\Exception $e){
                file_put_contents($this->baseUrl."order_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."order_log".date("Ym").".txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }
    }

    public function bind(){
        $data = request()->param();
        file_put_contents($this->baseUrl."bind_log.txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);

        if (is_string($data)) {
            $data = json_decode($data, true);
        }
        if (is_array($data)) {
            $data = $data;
        }

        $sn = $data['possn'];
        $equip = Lib::getTerminal($sn);
        if($equip){
            try {
                $bindTime = strtotime($data['bindTime']);

                $equip->active_time = $bindTime;
                $equip->binding_time = $bindTime;
                $equip->save();

//                Tools::operateApply($sn,$equip->user_id);//客服二部，处理完到店设备，绑定后自动完成设备申请

                Tools::sendBind($equip->user_id,date("Y-m-d H:i",$bindTime));

                \app\data\controller\Binds::instance()->customer_active($sn);
            }catch (\Exception $e){
                file_put_contents($this->baseUrl."bind_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."bind_log".date("Ym").".txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }

    }

    public function flow(){
        $data = request()->param();
        file_put_contents($this->baseUrl."flow_log.txt",json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);

        if (is_string($data)) {
            $data = json_decode($data, true);
        }
        if (is_array($data)) {
            $data = $data;
        }

        $sn = $data['possn'];
        $equip = Lib::getTerminal($sn);
        if($equip){
            try {
                $flow = new FlowInfo();
                $flow->sn = $sn;
                $flow->order_num = $data['orderId'];
                $flow->product_id = $equip->product_id;
                $flow->trading_time = strtotime($data['transTime']);
                $flow->flow_money = bcdiv($data['amount'],100,2);
                $flow->how_many = Lib::getFlowHowMany($sn) + 1;
                $flow->save();

                Tools::sendGift($sn);
            }catch (\Exception $e){
                file_put_contents($this->baseUrl."flow_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."flow_log".date("Ym").".txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }
    }

}
